package com.ahua.mallchat.common.common.event.listener;

import com.ahua.mallchat.common.common.event.UserRegisterEvent;
import com.ahua.mallchat.common.dao.UserDao;
import com.ahua.mallchat.common.domain.enums.user.IdempotentEnum;
import com.ahua.mallchat.common.domain.enums.user.ItemEnum;
import com.ahua.mallchat.common.domain.pojo.User;
import com.ahua.mallchat.common.service.IUserBackpackService;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 用户注册事件监听者
 */
@Component
public class UserRegisterListener {

    public static final int REG_TOP10 = 10;
    public static final int REG_TOP100 = 100;

    @Resource
    private IUserBackpackService userBackpackService;

    @Resource
    private UserDao userDao;

    /**
     * 发放改名卡
     */
    @EventListener(classes = UserRegisterEvent.class)
    public void sendCard(UserRegisterEvent event) {
        User user = event.getUser();
        userBackpackService.acquireItem(user.getId(), ItemEnum.MODIFY_NAME_CARD.getId(), IdempotentEnum.UID, user.getId().toString());
    }

    /**
     * 发放改名卡
     */
    @EventListener(classes = UserRegisterEvent.class)
    public void sendBadge(UserRegisterEvent event) {
        User user = event.getUser();
        int count = userDao.count();
        if (count <= REG_TOP10) {
            userBackpackService.acquireItem(user.getId(), ItemEnum.REG_TOP10_BADGE.getId(), IdempotentEnum.UID, user.getId().toString());
        } else if (count <= REG_TOP100) {
            userBackpackService.acquireItem(user.getId(), ItemEnum.REG_TOP10_BADGE.getId(), IdempotentEnum.UID, user.getId().toString());
        }
    }
}
